package com.isecommerce.framework.entity;

/**
 * Simple SQL String Entity<br><br>
 *
 * This class support for making simple SQL statement with given entity.
 * Used for implementation of ConfigExportEntity.
 *
 *
 *
 * @version 1.2.5
 * @author Leslie, Chen
 */
 // Leslie [2004.04.12]: Created.

 import java.util.*;
 import com.isecommerce.framework.util.Utility;

public class SimpleSQLStringEntity{

     protected ArrayList mapColumn;
     protected HashMap   mapValue;
     protected HashMap   mapUnknownValueCol;
     protected String tableName;

     protected final String CHANGABLE_SYMBOL   = "VAR$";

     /**
      * Constructor with given table.
      *
      * @param tableName DB table name.
      */
     public SimpleSQLStringEntity(String tableName){
         this.tableName = tableName;
         this.mapColumn = new ArrayList();
         this.mapValue  = new HashMap();
         this.mapUnknownValueCol = new HashMap();
     }

     /**
      * Clear all column.
      */
     public void clearColumn(){
         this.mapColumn.clear();
         this.mapValue.clear();
         this.mapUnknownValueCol.clear();
     }

     /**
      * Set table name for making SQL statement.
      *
      * @param tableName DB table name.
      */
     public void setTable(String tableName){
         this.tableName = tableName;
     }

     /**
      * Add field item without value.
      *
      * @param colName Field name.
      */
     public void addItem(String colName){
         addItem(colName, "?", false);
         this.mapUnknownValueCol.put(colName, colName);
     }

     /**
      * Add field item with given field name and value.
      *
      * @param colName Given field name.
      * @param value Given value.
      */
     public void addItem(String colName, String value){
         addItem(colName, value, false);
     }

     /**
      * Add field item with given field name and value, changable option.
      *
      * @param colName Given field name.
      * @param value Given value.
      * @param isChangableValue Field value changable option.
      */
     public void addItem(String colName, String value, boolean isChangableValue){

         if(isChangableValue)
             value += CHANGABLE_SYMBOL + value;

         // Oracle
         value = Utility.replaceAll(value, "'", "''");
         value = Utility.replaceAll(value, "\n", "");
         mapColumn.add(colName);
         mapValue.put(colName, value);
     }

     /**
      * Making INSERT query string.
      *
      * @return INSERT query string.
      */
     public String makeInsertStatement(){
         StringBuffer buf = new StringBuffer();
         StringBuffer colNames = new StringBuffer();
         StringBuffer values = new StringBuffer();

         boolean isFirst = true;


         String str = "";
         String val = "";

         for(int i=0; i<mapColumn.size(); i++){
             str = (String) mapColumn.get(i);

             if(this.mapUnknownValueCol.get(str) == null) val = "'" + (String) mapValue.get(str) + "'";
             else val = (String) mapValue.get(str);

             if(isFirst) isFirst = false;
             else {
                 str = "," + str;
                 val = "," + val;
             }

             colNames.append(str);
             values.append(val);
         }

         buf.append("INSERT INTO ");
         buf.append(this.tableName + " ");
         buf.append("(");
         buf.append(colNames.toString());
         buf.append(") ");
         buf.append("VALUES ");
         buf.append("(");
         buf.append(values.toString());
         buf.append(")\n");

         return buf.toString();
     }

}
